import { PropsWithChildren, ReactNode } from 'react';
import { Redirect } from "react-router-dom";
export interface ICanActivate {
    canActivate: () => boolean|Promise<Boolean>;
    Redirect: string;
}
export function AuthGuard(props: PropsWithChildren<ICanActivate>): any {
    console.log(props);
    let res = props.canActivate();
    if(typeof res == 'boolean') {
        if(res === true) {
            return props.children;
        } else {
            return <Redirect to={props.Redirect}/>
        }
    }
    if(res instanceof Promise) {
        res.then(can => {
            if(can === true) {
                return props.children;
            } else {
                return <Redirect to={props.Redirect}/>
            }
        });
    }
    throw new Error('代码编写错误');
}
